package com.dsh.code;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.mybatis.spring.SqlSessionTemplate;

import com.dsh.code.entity.Page;

public class PageHelper {
	public static Page getListByPage(String mapid,Page page,Map<String, Object> map) throws SQLException{
		
		MappedStatement mappedStatement = sqlSession.getConfiguration().getMappedStatement(mapid);
		BoundSql boundSql = mappedStatement.getBoundSql(map);
		String sql = boundSql.getSql();
		String totalNumSql = "select count(*) from ("+sql+")";
		StringBuffer limitSql = new StringBuffer(sql);
		if(page.getStart_num()!=null&&page.getEnd_num()!=null)
			limitSql.append(" limit "+page.getStart_num()+","+page.getEnd_num());
//		int totalNumSql = 
		
		System.out.println("totalNumSql:"+totalNumSql);
		System.out.println("limitSql:"+limitSql);
		
		Connection connection=mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection();
//		BoundSql countBS = copyFromBoundSql(mappedStatement, boundSql, countSql);
		PreparedStatement countStmt = connection.prepareStatement(totalNumSql); 
//		BoundSql countBS = copyFromBoundSql(mappedStatement, boundSql, totalNumSql);  
//		DefaultParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, parameterObject, countBS);  
		
		
		return page;
	}
	
	
	@Resource(name="sqlSession")
	private static SqlSessionTemplate sqlSession;
	
}
